package conson.tech.manage.facade.controller.coupons;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.coupons.entity.dto.*;
import conson.tech.manage.coupons.entity.vo.AppSeckillAndReceivceCoupons;
import conson.tech.manage.coupons.entity.vo.UserCouponDetailVo;
import conson.tech.manage.coupons.entity.vo.UserGrantCouponVo;
import conson.tech.manage.coupons.service.AppCouponsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.List;

/**
 * ClassName: CouponsAppController
 * Description: 页面配置分组配置app配置控制器
 * Author: HXL
 * Date: 2020/4/23 2:11 下午
 * Version: 1.0
 */
@Slf4j
@RestController
@Api(value = "优惠券对外接口", tags = "优惠券对外部接口")
@RequestMapping(SecurityConstants.MOBILEAPI + "/coupons")
public class AppCouponsController extends BaseController {

    @Autowired
    private AppCouponsService appCouponsService;

    @ApiOperation(value = "按活动id获取关联的优惠券列表")
    @ApiImplicitParam(name = "dto", value = "按活动id获取关联的优惠券列表参数", required = true, dataType = "CouponsListByActivityIdDto")
    @PostMapping("/coupon-list")
    public Response<Page<UserCouponDetailVo>> couponActivityList(@Valid @NotNull(message = "按活动id获取关联的优惠券列表入参") @ApiParam(value = "活动id", required = true) @RequestBody CouponsListByActivityIdDto dto) {

        log.info("AppCouponsController【按活动id获取关联的优惠券列表】--> inParam:【{}】", JSONUtil.toJsonStr(dto));
        return Response.success(this.appCouponsService.couponActivityList(dto.getPageIndex(), dto.getPageSize(), dto.getId()));
    }

    @ApiOperation(value = "获取用户我的优惠券")
    @ApiImplicitParam(name = "dto", value = "获取用户我的优惠券入参", required = true, dataType = "UserCouponListDto")
    @PostMapping("/my")
    public Response<Page<UserCouponDetailVo>> userCouponList(HttpServletRequest request,
                                                             @Valid @NotNull(message = "获取用户我的优惠券入参") @ApiParam(value = "活动id", required = true) @RequestBody UserCouponListDto dto) {

        log.info("AppCouponsController【获取用户我的优惠券】--> inParam:【{}】", JSONUtil.toJsonStr(dto));
//        Long currentUserId = this.getCurrentUserId(request);
        Long currentUserId = 123456789L;
        String currentUserName = "用户1";
        return Response.success(this.appCouponsService.userCouponList(currentUserId, dto.getPageIndex(), dto.getPageSize(), dto));
    }

    @ApiOperation(value = "获取用户优惠券配置信息")
    @ApiImplicitParam(name = "dto", value = "优惠券编码集合", required = true, dataType = "CouponDto")
    @PostMapping("/details")
    public Response<List<UserCouponDetailVo>> couponConfigDetails(HttpServletRequest request,
                                                                  @Valid @ApiParam(value = "优惠券编码（多个）", required = true) @RequestBody CouponDto dto) {

        log.info("AppCouponsController【根据优惠券编码获取优惠券配置信息】--> inParam:【{}】", JSONUtil.toJsonStr(dto));
//        Long currentUserId = this.getCurrentUserId(request);
        Long currentUserId = 123456789L;
        String currentUserName = "用户1";
        return Response.success(this.appCouponsService.couponConfigDetails(currentUserId, dto));
    }

    @ApiOperation(value = "用户领用优惠券（直接领取活动）")
    @ApiImplicitParam(name = "dto", value = "用户领用优惠券入参", required = true, dataType = "UserGrantCouponDto")
    @PostMapping("/grant")
    public Response<UserGrantCouponVo> grantCoupon(HttpServletRequest request, @Valid @ApiParam(value = "用户领用优惠券入参", required = true) @RequestBody UserGrantCouponDto dto) {

        log.info("AppCouponsController【用户领用优惠券】--> inParam:【{}】", JSONUtil.toJsonStr(dto));
//        Long currentUserId = this.getCurrentUserId(request);
//        Long currentUserId = ((long) ((Math.random() * 9 + 1) * 100000));
        Long currentUserId = 123456789L;
        String currentUserName = "用户1";
        return this.appCouponsService.grantCoupon(currentUserId, currentUserName, dto);
    }

    @ApiOperation(value = "用户领用优惠券(限时券活动或领券活动)")
    @ApiImplicitParam(name = "dto", value = "限时抢配置id或领券配置id", required = true, dataType = "ConfigDto")
    @PostMapping("/grant-time")
    public Response<UserGrantCouponVo> grantCouponTime(HttpServletRequest request,
                                                       @Valid @NotNull(message = "用户领用优惠券入参") @ApiParam(value = "用户领用优惠券入参", required = true) @RequestBody ConfigDto dto) {

//        log.info("AppCouponsController【用户领用优惠券】--> inParam:【{}】", JSONUtil.toJsonStr(dto));
//        Long currentUserId = this.getCurrentUserId(request);
//        Long currentUserId = ((long) ((Math.random() * 9 + 1) * 100000));
        Long currentUserId = 123456789L;
        String currentUserName = "用户1";
        return this.appCouponsService.grantCouponTime(currentUserId, currentUserName, dto.getId());
    }

    @ApiOperation(value = "领券中心用户优惠券列表-领券和秒杀(限时抢)")
    @PostMapping("/list")
    public Response<AppSeckillAndReceivceCoupons> queryCoupons() throws ParseException {

        log.info("领券中心用户优惠券列表-领券和秒杀(限时抢)");
        Long currentUserId = 123456789L;
        String currentUserName = "用户1";
        return Response.success(this.appCouponsService.queryCoupons(currentUserId));
    }
}
